package com.mhd.databases.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author dengminghao
 * @version 1.0.0
 * @ClassName ReadFileUtils.java
 * @Description 读取本地sql文件
 * @createTime 2020/05/24 13:49
 */
public class ReadFileUtils {

    private final static Logger logger = LoggerFactory.getLogger(ReadFileUtils.class);

    public static String MYSQL_URL = "sql/mysql.sql";
    public static String ORACLE_URL = "sql/oracle.sql";

    public static String readSql(String sqlPath) {
//        ClassPathResource classPathResource = null;
//        if (JdbcUtils.MYSQL.equalsIgnoreCase(sqlType)) {
//            classPathResource = new ClassPathResource(MYSQL_URL);
//        }else{
//            classPathResource = new ClassPathResource(ORACLE_URL);
//        }
        ClassPathResource classPathResource = new ClassPathResource(sqlPath);
        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader br = null;
        InputStream inputStream = null;
        try {
            inputStream = classPathResource.getInputStream();
            br = new BufferedReader(new InputStreamReader(inputStream));
            String resString = new String("");
            while ((resString = br.readLine()) != null) {
                stringBuffer.append(resString);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                inputStream.close();
                System.out.println(stringBuffer.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuffer.toString();
    }
}
